package com.ryan.route.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.route.entity.TravelAdvisor;
import com.ryan.route.service.TravelAdvisorService;
import com.ryan.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 定制师 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2020-07-24
 */
@RestController
@RequestMapping("/route/advisor")
public class TravelAdvisorController {

    /**
     * 注入TravelAdvisorService
     */
    @Autowired
    private TravelAdvisorService advisorService;

    /**
     * 获取所有定制师
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/getAdvisor/{pageIndex}/{pageSize}")
    public R getAllAdvisor(@PathVariable long pageIndex,
                           @PathVariable long pageSize,
                           @RequestParam(required = false) String name){
        //List<TravelAdvisor> list = advisorService.list();
        Page<TravelAdvisor> resultPage = advisorService.queryPage(pageIndex, pageSize, name);
        return R.ok().data("list", resultPage.getRecords()).data("totalPage", resultPage.getTotal());
    }

    /**
     * 增加定制师
     * @param advisor 定制师实体
     * @return 如果添加成功则返回ok，如果添加失败则返回error
     */
    @PostMapping("/addAdvisor")
    public R addAdvisor(@RequestBody TravelAdvisor advisor){
        boolean save = advisorService.save(advisor);
        return save?R.ok():R.error();
    }

    /**
     * 根据定制师id获取定制师信息
     * @param id 定制师id
     * @return 统一结果集
    */
    @GetMapping("/getAdvisorById/{id}")
    public R getAdvisorById(@PathVariable String id){
        TravelAdvisor advisor = advisorService.getById(id);
        return R.ok().data("advisor", advisor);
    }

    /**
     * 更新定制师信息
     * @param advisor
     * @return
     */
    @PostMapping("/updateAdvisor")
    public R updateAdvisor(@RequestBody TravelAdvisor advisor){
        boolean update = advisorService.updateById(advisor);
        return update?R.ok():R.error();
    }

    /**
     * 批量删除定制师
     * @param advisorIds
     * @return
     */
    @PostMapping("/deleteAdvisor")
    public R deleteAdvisor(@RequestBody Long[] advisorIds){
        boolean delete = advisorService.removeByIds(Arrays.asList(advisorIds));
        return delete?R.ok():R.error();
    }

    /**
     * 根据名称获取记录数，主要用户数据校验，用户名不能重复
     * @param name
     * @return
     */
    @GetMapping("/getAdvisorByName")
    public int getAdvisorByName(@RequestParam String name){
        QueryWrapper<TravelAdvisor> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        int count = advisorService.count(wrapper);
        return count;
    }


}

